﻿using Excel2DbTool.Common;
using MvvmFoundation.Wpf;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Excel2DbTool.Helpers;
using System.Windows;
using System.ComponentModel;
using System.IO;

namespace Excel2DbTool.ViewModels
{
    public class MainViewModel : ObservableObject
    {
        String lastSelectDirPath = String.Empty;

        public MainViewModel()
        {
            RegisiterMessenger();

            FilePaths = new ObservableCollection<FilePathViewModel>() { };

            SelectFileCommand = new RelayCommand<Object>((obj) =>
            {
                IsPrompt = false;

                Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog()
                {

                    //Multiselect = true,
                    FileName = "信息",
                    DefaultExt = ".xlsx",
                    Filter = "Excel 工作薄 (.xlsx)|*.xlsx",
                    InitialDirectory = String.IsNullOrEmpty(lastSelectDirPath) ? @"D:\Docs\ExcelOperateProject" : lastSelectDirPath,
                    CheckFileExists = true
                };

                if (ofd.ShowDialog() == true)
                {
                    string selectedFullPath = ofd.FileName;
                    lastSelectDirPath = Directory.GetParent(selectedFullPath).FullName;


                    FilePaths.AddRange(ofd.FileNames.Select(fPath => { return new FilePathViewModel(fPath); }).ToList());
                }
            }, (obj) =>
            {
                return (FilePaths.Count == 0) && FilePaths.All(fp => !fp.IsProccessing);
            });

            ExecuteConvertCommand = new RelayCommand<Object>((obj) =>
            {
                List<String> exceptionStrs = new List<string>(10);
                foreach (var filePath in FilePaths)
                {
                    try
                    {
                        filePath.Execute();
                    }
                    catch (Excel2DbException ex)
                    {
                        exceptionStrs.Add(ex.Message);
                    }
                }

            }, (obj) =>
            {
                return (FilePaths.Count == 1) && FilePaths.All(fp => !fp.IsProccessing);
            });

            ClearCommand = new RelayCommand<Object>((obj) =>
            {
                FilePaths.Clear();
                foreach (var item in FilePaths)
                {
                    item.Reset();
                }
            }, (obj) =>
            {
                return (FilePaths.Count > 0 && FilePaths.All(fp => !fp.IsProccessing));
            });
        }

        void RegisiterMessenger()
        {
            App.Messenger.Register("Done", () =>
            {
                ClearCommand.Execute(null);
                MessageBox.Show("转换完成！");
            });

            App.Messenger.Register("Fail", () =>
            {
                MessageBox.Show("转换失败!,请检查应用程序下的Log目录!");
            });
        }

        private Boolean isPrompt = true;
        public Boolean IsPrompt
        {
            get
            {
                return isPrompt;
            }
            set
            {
                if (isPrompt == value)
                {
                    return;
                }

                isPrompt = value;

                RaisePropertyChanged("IsPrompt");
            }
        }


        public RelayCommand<Object> SelectFileCommand { get; private set; }
        public RelayCommand<Object> ExecuteConvertCommand { get; private set; }
        public RelayCommand<Object> ClearCommand { get; private set; }


        public ObservableCollection<FilePathViewModel> FilePaths { get; private set; }
    }
}
